package com.htu.service.Impl;

import com.htu.mapper.BusinessMapper;
import com.htu.mapper.CartMapper;
import com.htu.mapper.OrderDetailetMapper;
import com.htu.mapper.OrdersMapper;
import com.htu.po.Business;
import com.htu.po.Cart;
import com.htu.po.OrderDetailet;
import com.htu.po.Orders;
import com.htu.service.OrdersService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private CartMapper cartMapper;
    @Autowired
    private OrderDetailetMapper orderDetailetMapper;

    @Transactional
    public int createOrders(String userId, Integer businessId, Integer daId) {
     int orderId=0;

        Cart cart = new Cart();
        cart.setBusinessId(businessId);
        cart.setUserId(userId);
        List<Cart> cartList = cartMapper.listCart(cart);
        Business business = businessMapper.getBusinessById(businessId);
        Double ordersTotal = 0.0;
        for (Cart c : cartList){
            ordersTotal += c.getFood().getFoodPrice() * c.getQuantity();
        }
        ordersTotal += business.getDeliveryPrice();

        Orders orders = new Orders();
        orders.setUserId(userId);
        orders.setBusinessId(businessId);
        orders.setOrderTotal(ordersTotal);
        orders.setDaId(daId);

         ordersMapper.saveOrders(orders);
          orderId= orders.getOrderId();
        List<OrderDetailet> orderDetailetList = new ArrayList<OrderDetailet>();

        for (Cart c : cartList){
            OrderDetailet od = new OrderDetailet();
            od.setOrderId(orderId);
            od.setFoodId(c.getFoodId());
            od.setQuantity(c.getQuantity());
            orderDetailetList.add(od);
        }
        orderDetailetMapper.saveOrderDetailetBatch(orderDetailetList);
        System.out.println("success.....");
        cartMapper.removeCart(cart);
        return orderId;
    }

    public Orders getOrdersById(Integer orderId) {
        Orders orders = null;
        try {
            orders = ordersMapper.getOrdersById(orderId);
            List<OrderDetailet> list = orderDetailetMapper.listOrderDetailetByOrderId(orderId);
            orders.setOdList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orders;
    }

    public List<Orders> listOrderByUserId(String userId) {
        List<Orders> list = new ArrayList<Orders>();
        list  = ordersMapper.listOrderByUserId(userId);
        for (Orders o : list){
            List<OrderDetailet> odList = orderDetailetMapper.listOrderDetailetByOrderId(o.getOrderId());
            o.setOdList(odList);
        }
        return list;
    }

    @Override
    public int updateOrderStateByOrderId(int orderId) {
        return ordersMapper.updateOrderStateByOrderId(orderId);
    }
}
